package com.powervision.thirdlogindemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.facebook.CallbackManager
import com.facebook.FacebookSdk
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.tasks.Task
import com.powervision.thirdlogindemo.bean.FaceBookLoginInfoBean
import com.powervision.thirdlogindemo.utils.CheckApkExistUtils
import com.powervision.thirdlogindemo.utils.CheckGoogleServiceUtils
import com.powervision.thirdlogindemo.utils.FaceBookLoginUtils
import com.powervision.thirdlogindemo.utils.GoogleLoginUtils
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), GoogleLoginUtils.GoogleLoginListener,
    FaceBookLoginUtils.FaceBookLoginListener {
    private val mFaceBookLoginUtils by lazy {
        FaceBookLoginUtils(
            this,
            callbackManager = mCallbackManager
        )
    }
    private val mCallbackManager by lazy { CallbackManager.Factory.create() }
    private val TAG by lazy { "MainActivity" }
    private val mGoogleLoginUtils by lazy { GoogleLoginUtils(this, this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initFaceBook()
        initGoogle()
    }

    private fun initGoogle() {
        mGoogleLoginUtils.setGoogleSignListener(this)
    }

    private fun initFaceBook() {
        try {
            mFaceBookLoginUtils.setFaceBookLoginListener(this)
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    private fun initView() {
        tv_google.setOnClickListener {
            checkGoogleService()
        }

        tv_facebook.setOnClickListener {
            if (!CheckApkExistUtils.checkFacebookExist(this)) {
                Toast.makeText(this, "请先安装FaceBook客户端", Toast.LENGTH_LONG).show()
            } else {
                mFaceBookLoginUtils.login()
            }
        }
        tv_twitter.setOnClickListener {

        }
    }

    /**
     * 先检查用户是否安装Google服务
     */
    private fun checkGoogleService() {
        val code = CheckGoogleServiceUtils.checkGooglePlayServiceExist(this)
        if (code == ConnectionResult.SUCCESS) {
            mGoogleLoginUtils.signIn()
        } else {
            code.let { CheckGoogleServiceUtils.onCheckGooglePlayServices(this, it) }
        }
    }

    override fun onLoginSuccess(infoBean: FaceBookLoginInfoBean) {
        //处理Facebook登录成功后的请求
    }

    override fun onLoginFailed(message: String) {
    }

    override fun onLoginCancel(message: String) {
    }

    override fun onLogoutSuccess() {
    }

    override fun onLogoutFailed() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        Log.d(TAG, resultCode.toString())
        if (mCallbackManager != null) {
            mCallbackManager.onActivityResult(requestCode, resultCode, data)
        }
        if (requestCode == mGoogleLoginUtils.requestCode) {
            val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
            mGoogleLoginUtils.handleSignInResult(task)
        }
    }

    override fun googleLoginSuccess(account: GoogleSignInAccount) {
        //处理Google登录成功后的请求
    }

    override fun googleLoginFail(message: String?) {
    }

    override fun googleLogoutSuccess() {
    }

    override fun googleLogoutFail() {
    }
}